package com.kaibes.module.user;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.core.base.annotation.Description;
import com.kaibes.module.user.pojo.User;
import com.kaibes.module.user.pojo.UserInfoUpdate;
import com.kaibes.module.user.pojo.UserRoleUpdate;
import com.kaibes.module.user.pojo.UserSelfUpdate;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/users")
public class UserApi extends ResponseApi<User, UserService> {


    @DeleteMapping
    @ApiLink("删除用户，根据id")
    public ResponseEntity<Object> delete(@RequestBody @Description("用户ID数组") List<Long> ids) {
        if (ids.size() == 1 && ids.get(0) == 1L) {
            return ResponseFactory.deleteFailure("这个是固定的系统管理员,您不能删除这个用户");
        } else {
            ids.removeIf(value -> value == 1);
            return super.deleteByIds(ids, "删除用户成功", "输出用户失败");
        }
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiLink("删除用户，根据id")
    public ResponseEntity<Object> delete(@PathVariable @Description("用户ID") Integer id) {
        if (id == 1L) {
            return ResponseFactory.deleteFailure("这个是固定的系统管理员,您不能删除这个用户");
        } else {
            return super.deleteById(id, "删除用户成功", "删除用户失败");
        }
    }

    @PutMapping("/{id:\\d+}")
    @ApiLink("修改用户信息，根据id")
    public ResponseEntity<Object> put(@PathVariable @Description("用户ID") Integer id, @RequestBody @Validated UserInfoUpdate user) {
        return super.putById(user, id, "用户信息修改成功", "用户信息修改失败");
    }
    
    @PutMapping("/self")
    @ApiLink(value = "修改用户信息", validated = false)
    public ResponseEntity<Object> putSelf(@RequestAttribute @Description("用户ID") Integer userId, @RequestBody @Validated UserInfoUpdate user) {
        return super.putById(user, userId, "用户信息修改成功", "用户信息修改失败");
    }
    
    @PutMapping("/role/{id:\\d+}")
    @ApiLink("修改用户角色，根据id")
    public ResponseEntity<Object> put(@PathVariable @Description("用户ID") Integer id, @RequestBody @Validated UserRoleUpdate user) {
        return super.putById(user, id, "用户角色修改成功", "用户角色修改失败");
    }
    
    // ======================
    @PutMapping
    @ApiLink(value = "修改自己的信息", validated = false)
    public ResponseEntity<Object> putSelf(@RequestAttribute Integer userId, @RequestBody @Validated UserSelfUpdate user) {
        return super.putById(user, userId, "用户信息修改成功", "用户相信修改失败");
    }
    
    @GetMapping
    @ApiLink(value = "获取自身用户信息", validated = false)
    public ResponseEntity<User> getSelf(@RequestAttribute Integer userId) {
        return super.getById(userId, "获取用户信息成功", "获取用户信息失败");
    }
    
    @GetMapping("/{userId:\\d+}")
    @ApiLink(value = "获取用户信息", validated = false)
    public ResponseEntity<User> getUser(@PathVariable Integer userId) {
        return super.getById(userId, "获取用户信息成功", "获取用户信息失败");
    }

}
